
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ui;

import data.LocalizationSupport;
import data.MTPreferences;
import fileselectdialog.MTFileSelectDialog;
import fileselectdialog.MTFileSelectDialogListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

/**
 *
 * @author Tyrael
 */
public class MTListTorrents extends List implements CommandListener, MTFileSelectDialogListener
{
    public static final int ScreenID = 1;

    private MTUIManager uiManager;
    private Command command_Exit;
    private Command command_AddTorrent;
    private Command command_Settings;
    private Command command_DownloadState;
    private Command command_Start;
    private Command command_Stop;
    private Command command_Remove;
    private Command command_Files;
    private Command command_Properties;
    private Command command_About;
    private Command command_Help;

    public MTListTorrents(MTUIManager aUIManager)
    {
        super(LocalizationSupport.getMessage("title_listTorrents"),List.IMPLICIT);
        uiManager = aUIManager;
        command_Exit = new Command(LocalizationSupport.getMessage("command_exit"), Command.EXIT, 0);
        command_AddTorrent = new Command(LocalizationSupport.getMessage("command_addTorrent"), Command.SCREEN, 1);
        command_Settings = new Command(LocalizationSupport.getMessage("command_settings"), Command.SCREEN, 2);
        command_DownloadState = new Command(LocalizationSupport.getMessage("command_downloadstate"), Command.SCREEN, 3);
        command_Start = new Command(LocalizationSupport.getMessage("command_start"), Command.SCREEN, 4);
        command_Stop = new Command(LocalizationSupport.getMessage("command_stop"), Command.SCREEN, 5);
        command_Remove = new Command(LocalizationSupport.getMessage("command_remove"), Command.SCREEN, 6);
        command_Files = new Command(LocalizationSupport.getMessage("command_files"), Command.SCREEN, 7);
        command_Properties = new Command(LocalizationSupport.getMessage("command_properties"), Command.SCREEN, 8);
        command_About = new Command(LocalizationSupport.getMessage("command_about"), Command.SCREEN, 9);
        command_Help = new Command(LocalizationSupport.getMessage("command_help"), Command.SCREEN, 10);
        addCommand(command_Exit);
        addCommand(command_AddTorrent);
        addCommand(command_Settings);
        addCommand(command_DownloadState);
        addCommand(command_Start);
        addCommand(command_Stop);
        addCommand(command_Remove);
        addCommand(command_Files);
        addCommand(command_Properties);
        addCommand(command_About);
        addCommand(command_Help);
        setSelectCommand(command_DownloadState);
        setCommandListener(this);

        setCommandsByState();
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == command_Exit)
        {
            uiManager.exit();
        }
        else if (c == command_Settings)
        {
            uiManager.showScreen(MTFormSettings.ScreenID);
        }
        else if (c == command_AddTorrent)
        {
            if (!MTPreferences.DownloadFolder.equals(""))
            {
                String[] torrentExtensions = {".torrent",".tor"};
                MTFileSelectDialog torrentSelectDialog = new MTFileSelectDialog(
                    Display.getDisplay(uiManager.getMidlet()),this);

                torrentSelectDialog.showFileSelectDialog(torrentExtensions,true);
            }
            else
            {
                uiManager.showMessage(LocalizationSupport.getMessage("label_selectDownloadFolderFirst"));
            }
        }
        else if (c == command_DownloadState)
        {
            uiManager.showScreen(MTCanvasDownloadState.ScreenID);
        }
        else if (c == command_Start)
        {
            uiManager.startTorrent(getSelectedIndex());
        }
        else if (c == command_Stop)
        {
            uiManager.stopTorrent(getSelectedIndex());
        }
        else if (c == command_Remove)
        {
            uiManager.removeTorrent(getSelectedIndex());
        }
        else if (c == command_Properties)
        {
            uiManager.showScreen(MTFormProperties.ScreenID);
        }
        else if (c == command_Files)
        {
            uiManager.showScreen(MTListFiles.ScreenID);
        }
        else if (c == command_About)
        {
            uiManager.showScreen(MTFormAbout.ScreenID);
        }
        else if (c == command_Help)
        {
            uiManager.showScreen(MTFormHelp.ScreenID);
        }
    }

    // set or remove commands in the Torrents lists
    public void setCommandsByState()
    {
        if (size()>0)
        {
            addCommand(command_Start);
            addCommand(command_Stop);
            addCommand(command_DownloadState);
            // need to set the default select command again
            setSelectCommand(command_DownloadState);
            addCommand(command_Files);
            addCommand(command_Properties);
            addCommand(command_Remove);
        }
        else
        {
            // the empty torrents list does not conatin all command
            removeCommand(command_Start);
            removeCommand(command_Stop);
            removeCommand(command_DownloadState);
            removeCommand(command_Files);
            removeCommand(command_Properties);
            removeCommand(command_Remove);
            setTitle(LocalizationSupport.getMessage("title_listTorrents"));
        }
    }

    public void fileSelected(String aSelectedFileName, String aSelectedFileNameWithoutPath) {
        uiManager.show(this);
        uiManager.openNewTorrent(aSelectedFileName,aSelectedFileNameWithoutPath,
                false,null,MTPreferences.DownloadFolder);
    }

    public void folderSelected(String aSelectedFolderName, String aSelectedFolderNameWithoutPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void selectionCancelled() {
        uiManager.show(this);
    }
}

